<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/aui.css" />
    <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>
    <header class="header header-f" style="padding-top:2rem; height:4rem;">
        <div class="header-l aui-pull-left" onclick="closeWin()">
            <i class="fa fa-angle-left"></i>
        </div>
        <div class="header-title">
            优惠券领取中心
        </div>
        <div class="header-r aui-pull-right" tapmode onclick="userindex()">
            <i class="fa fa-user-o"></i>
        </div>
    </header>
    <div style="height:4rem;"></div>

    <ul class="distribution-withdraw-nav" id="list2">

    </ul>
    
    <div id="list">

    </div>


</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/template" id="template2">
    {{~it:value:index}}
    <li id="{{=value.class_id}}" onclick="changeSort({{=value.class_id}});">{{=value.name}}</li>
    {{~}}
</script>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="coupon">
        <div class="coupon-l">
            ￥<span>{{=value.money}}</span>
            <p>{{=value.class_name}}</p>
        </div>
        <div class="coupon-m"></div>
        <dl class="coupon-r" tapmode onclick="coupondetails({{=value.coupon_id}})">
            <dt>
                {{=value.name}}
                <p>消费满{{=value.meet_money}}元立减{{=value.money}}</p>
            </dt>
            <dd>
                <p>剩余99/100张</p>
                有效期(天)：{{=value.expiry_day}}
                <!-- 有效期(天)：2018-11-01至2018-12-01 -->
            </dd>
        </dl>
        {{? value.receive}}
        <div class="coupon-btn">已领取</div>
        {{??}}
        <div class="coupon-btn" onclick="getCoupon({{=value.coupon_id}});">立即领取</div>
        {{?}}
    </div>
    {{~}}
</script>
<script type="text/javascript">
    apiready = function(){

        loadCouponSort();
    }

    function loadCouponSort(){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Coupon/classList',
            method: 'get',
            data: {
                values: {
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                templateFun2(ret.result.classList);
                couponSort = ret.result.classList[0].class_id;
                $('#'+couponSort).addClass('active');
                loadCoupon(couponSort);
            } else {

                // alert( JSON.stringify( err ) );
                var data = new Array();
                templateFun2(data);

                return false;
            }
        });

    }

    function loadCoupon(classid){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Coupon/index',
            method: 'get',
            data: {
                values: {
                    limit:limit,
                    type: 'class',
                    class_id: classid,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
                'Content-Type': 'application/x-www-form-urlencoded',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                templateFun(ret.result.data);
            } else {

                // alert( JSON.stringify( err ) );
                var data = new Array();
                templateFun(data);

                return false;
            }
        });
    }

    // 模板赋值函数(优惠券分类)
    function templateFun2(data){
        if (data.length) {
            var template = $api.byId('template2');
            var dotFn = doT.template(template.innerHTML);
            var html = dotFn(data);
            var list = $api.byId('list2');
            $api.html(list, html);
        }
    }

    // 模板赋值函数(优惠券列表)
    function templateFun(data){
        if (data.length) {
            var template = $api.byId('template');
            var dotFn = doT.template(template.innerHTML);
            var html = dotFn(data);
            var list = $api.byId('list');
            $api.html(list, html);
        }else{
            var list = $api.byId('list');
            var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:1rem;">暂无内容</h2>';
            list.innerHTML = html;
        }
    }

    // 领取优惠券
    function getCoupon(couponid){
        var userid = $api.getStorage('userid');
        var authtoken = $api.getStorage('authtoken');
        api.ajax({
            url: webAddress + '/a/order/Coupon/receive',
            method: 'post',
            data: {
                values: {
                    id: couponid,
                },
            },
            headers: {
                TOKEN: token,
                authuid: userid,
                authtoken: authtoken,
                // 'Content-Type': 'application/json',
            },
        },function(ret, err){
            if (ret) {
                alert( JSON.stringify( ret ) );
                api.toast({
                    msg: ret.message,
                    duration: 2000,
                    location: 'bottom',
                    global: 'true',
                });
                loadCoupon(couponSort);
            } else {
                // alert( JSON.stringify( err ) );
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom',
                    global: 'true',
                });
                return false;
            }
        });
    }

    // 点击切换类别
    function changeSort(classid){
        // 全局变量记录现在分类
        couponSort = classid;
        $('li').removeClass('active');
        $('#'+classid).addClass('active');

        loadCoupon(classid);
    }

    function coupondetails(){
        var delay = 0;
        if(api.systemType != 'ios'){
            delay = 300;
        }
        api.openWin({
            name: 'coupondetails',
            url: './coupon_details.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
                type:"page"
            }
        });
    };

    function userindex(){
        var delay = 0;
        if(api.systemType != 'ios'){
            delay = 300;
        }
        api.openWin({
            name: 'userindex',
            url: './user-index.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
                type:"page"
            }
        });
    };

    function closeWin(){
        api.closeWin({
        });
    }
</script>
</html>
